package com.mayikt;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: maplefire
 * @Description: 自定义线程池
 * @Date: 2022/09/02 16:12
 */
class TaskThread implements Runnable {

    private String threadName;

    public TaskThread(String threadName) {
        this.threadName = threadName;
    }

    public void run() {
        System.out.println(Thread.currentThread().getName() + threadName);
    }
}


public class Test04 {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 2, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(3));
        threadPoolExecutor.execute(new TaskThread("任务1")); // 创建任务并执行
        threadPoolExecutor.execute(new TaskThread("任务2")); // 进入缓存队列
        threadPoolExecutor.execute(new TaskThread("任务3")); // 进入缓存队列
        threadPoolExecutor.execute(new TaskThread("任务4")); // 进入缓存队列
        threadPoolExecutor.execute(new TaskThread("任务5")); // 创建任务并执行
        threadPoolExecutor.execute(new TaskThread("任务6")); // 超出最大线程数，报错
    }
}
